#include "delay.h"
#include "sys.h"
#include "FreeRTOS.h"
#include "task.h"

#define HCLK_CLOCK_US (72-1)
#define HCLK_CLOCK_MS (72000-1)

int systick_init()
{
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

    return 0;
}

void delay_base(u32 delay_t, u32 load_base)
{
    u32 temp = 0;

    SysTick->LOAD = delay_t * load_base;
    SysTick->VAL = 0x00;
    SysTick->CTRL = 0x01;
    do {
        temp = SysTick->CTRL;
    } while ((temp & 0x1) && !(temp & (1 << 16)));

}

void delay_ms(u32 delay_t)
{
//    delay_base(delay_t, HCLK_CLOCK_MS);

	delay_us(delay_t * 1000);
}


void delay_us(u32 delay_t)
{
//    delay_base(delay_t, HCLK_CLOCK_US);
	for(uint32_t i=0;i<36*delay_t;i++);
	
}
